package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.ReportMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.vo.BusinessReportVo;
import com.itheima.health.vo.ReportSetmemlVo;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl extends ServiceImpl  implements ReportService {

//    @Reference
//    private MemberService memberService;

    @Autowired
    private  ReportMapper reportMapper;


    @Override
    public List<Map> getSexReport() {
        return reportMapper.getSexReport();
    }


    @Override
    public List<Map> getAgeReport() {
        return reportMapper.getAgeReport();
    }

    @Override
    public Map findLastyearMemberCountsByMonth(String startTime,String endTime) {
        List<Map>mapList=reportMapper.findLastyearMemberCountsByMonth(startTime,endTime);
        Map map=changeData(mapList);
        return map;
    }

//    @Override
//    public List<Map> getSetmealProportion() {
//
//        return reportMapper.getSetmealProportion();
//    }

    @Override
    public BusinessReportVo findBusinessData() {
        BusinessReportVo businessReportVo = new BusinessReportVo();
        String s = DateUtils.parseDate2String(new Date(), "yyyy-MM-dd");
        businessReportVo.setReportDate(s);
        businessReportVo.setTodayNewMember(reportMapper.todayNewMember());
        businessReportVo.setTotalMember(reportMapper.totalMember());
        businessReportVo.setThisWeekNewMember(reportMapper.thisWeekNewMember());
        businessReportVo.setThisMonthNewMember(reportMapper.thisMonthNewMember());
        businessReportVo.setTodayOrderNumber(reportMapper.todayOrderNumber());
        businessReportVo.setTodayVisitsNumber(reportMapper.todayVisitsNumber());
        businessReportVo.setThisWeekOrderNumber(reportMapper.thisWeekOrderNumber());
        businessReportVo.setThisWeekVisitsNumber(reportMapper.thisWeekVisitsNumber());
        businessReportVo.setThisMonthOrderNumber(reportMapper.thisMonthOrderNumber());
        businessReportVo.setThisMonthVisitsNumber(reportMapper.thisMonthVisitsNumber());
        List<ReportSetmemlVo> reportSetmemlVoList = reportMapper.findHotSetmeal();
        for (ReportSetmemlVo reportSetmemlVo : reportSetmemlVoList) {
            NumberFormat numberFormatormat = NumberFormat.getInstance();
            numberFormatormat.setMaximumFractionDigits(2);
            String result = numberFormatormat.format((float)(reportSetmemlVo.getSetmealCount()) / (float)reportMapper.findAllOrder()*100);
            reportSetmemlVo.setProportion(result);
        }
        businessReportVo.setHotSetmeal(reportSetmemlVoList);
        return businessReportVo;
    }

    private Map changeData(List<Map> mapList) {
        List<String> yearAndMonthList = new ArrayList<String>();
        List<Long> memberCountList = new ArrayList<Long>();
        Map map = new HashMap();
        for (Map map1 : mapList) {
            yearAndMonthList.add((String)map1.get("yearAndMonth"));
            memberCountList.add((Long)map1.get("memberCounts"));
        }
        map.put("memberCounts",memberCountList);
        map.put("yearAndMonth",yearAndMonthList);
        return map;
    }
}
